﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Exam Exercise 2</title>
    <script>
        function Solve(args) {
            var firstLine = args[0].split(" ");
            var secondLine = args[1].split(" ");
            var N = parseInt(firstLine[0]);
            var M = parseInt(firstLine[1]);
            var startRow = parseInt(secondLine[0]);
            var startCow = parseInt(secondLine[1]);

            var matrixValues = [];
            var matrixDir = [];

            var currValue = 1;
            for (var idxRow = 0; idxRow < N; idxRow++)
            {
                matrixValues[idxRow] = [];
                matrixDir[idxRow] = [];
                for (var idxCow = 0; idxCow < M; idxCow++)
                {
                    matrixValues[idxRow][idxCow] = currValue;
                    currValue++;

                    matrixDir[idxRow][idxCow] = args[idxRow + 2][idxCow];
                }
            }

            var currentRow = startRow;
            var currentCow = startCow;
            var pathSum = 0;
            var pathCells = 0;
            var ImLost = false;

            while ((currentRow >= 0) && (currentRow < N)
                && (currentCow >= 0) && (currentCow < M)
                && (false == ImLost))
            {
                if (0 == matrixValues[currentRow][currentCow])
                {
                    ImLost = true;
                }
                else
                {
                    pathSum += matrixValues[currentRow][currentCow];
                    pathCells++;
                    matrixValues[currentRow][currentCow] = 0;

                    if (matrixDir[currentRow][currentCow] == "l") {
                        currentCow -= 1;
                    }
                    else if (matrixDir[currentRow][currentCow] == "r") {
                        currentCow += 1;
                    }
                    else if (matrixDir[currentRow][currentCow] == "u") {
                        currentRow -= 1;
                    }
                    else if (matrixDir[currentRow][currentCow] == "d") {
                        currentRow += 1;
                    }
                }
            }

            if (ImLost == false)
            {
                console.log("out " + pathSum);
            }
            else
            {
                console.log("lost " + pathCells);
            }

            return "";
        }

        function execute() {
            // test 1
            var args = [];
            args[0] = "3 4";
            args[1] = "1 3";
            args[2] = "lrrd";
            args[3] = "dlll";
            args[4] = "rddd";
            Solve(args);

            // test 2
            var args = [];
            args[0] = "1 1";
            args[1] = "0 0";
            args[2] = "r";
            Solve(args);

            // test 3
            var args = [];
            args[0] = "5 8";
            args[1] = "0 0";
            args[2] = "rrrrrrrd";
            args[3] = "rludulrd";
            args[4] = "durlddud";
            args[5] = "urrrldud";
            args[6] = "ulllllll";
            Solve(args);

            // test 4
            var args = [];
            args[0] = "5 8";
            args[1] = "0 0";
            args[2] = "rrrrrrrd";
            args[3] = "rludulrd";
            args[4] = "lurlddud";
            args[5] = "urrrldud";
            args[6] = "ulllllll";
            Solve(args);
        }
    </script>
</head>
<body onload="execute()">
        
</body>
</html>
